package com.mfp.java.testYou.ui.mvc;

import java.util.LinkedList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mfp.java.testYou.core.AppContext;
import com.mfp.java.testYou.core.servlets.ManageQuestionServlet;
import com.mfp.java.testYou.core.utils.FormatUtils;
import com.mfp.java.testYou.db.model.Question;
import com.mfp.java.testYou.model.UiSelectPair;

public class QuestionsPage extends AbstractMVC {

	public static final String URL = "/testYou/pages/questions.jsp";
	
	private static final Integer LIST_SIZE = 10;
	private static final Integer LIST_ELEMT_LENGHT = 50;

	public QuestionsPage(HttpServletRequest request, HttpServletResponse response) {
		super(request, response);
		prepareList();
	}
	
	private void prepareList(){
		List<Question> list = AppContext.getDAO().getAllQuestions();
		List<UiSelectPair> uiList = new LinkedList<UiSelectPair>();
		for (Question q : list){
			String text = FormatUtils.removeTags(q.getText(), LIST_ELEMT_LENGHT);
			uiList.add(new UiSelectPair(text,q.getId()));
		}
		
		getRequest().setAttribute(SelectListComponent.ATTR_LIST_DATA, uiList);
		getRequest().setAttribute(SelectListComponent.ATTR_LIST_NAME, ManageQuestionServlet.INPUT_QUESTIONID);
		getRequest().setAttribute(SelectListComponent.ATTR_LIST_SIZE, LIST_SIZE);
	}

}
